using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using GraphAlgsPresentation.Core;
using System.Xml.Serialization;
using System.ComponentModel;

namespace GraphAlgsPresentation.ScriptLibrary
{
    /// <summary>
    /// Provides a change vertices fill color
    /// </summary>
    [Serializable]
    public class ChangeVerticesFillColor : Command
    {
        public class NameColorPair
        {
            string _name;
            Color _color;
            /// <summary>
            /// Gets or sets the name.
            /// </summary>
            /// <value>The name.</value>
            public string Name { get { return _name; } set { _name = value; } }
            /// <summary>
            /// Gets or sets the color.
            /// </summary>
            /// <value>The color.</value>
            public Color Color { get { return _color; } set { _color = value; } }
        }
        
        private List<string> names;
        private List<Color> old;
        private List<string> colors;

        List<NameColorPair> _values = new List<NameColorPair>();
        /// <summary>
        /// Gets or sets the values.
        /// </summary>
        /// <value>The values.</value>
        [XmlIgnore]
        public List<NameColorPair> Values
        {
            get { return _values; }
            set { _values = value; }
        }

        /// <summary>
        /// Gets or sets the names.
        /// </summary>
        /// <value>The names.</value>
        [Browsable(false)]
        public List<string> Names
        {
            get
            {
                if (this.Values.Count == 0)
                    return names;
                else
                {
                    List<string> res = new List<string>();
                    foreach (NameColorPair pair in this.Values)
                        res.Add(pair.Name);
                    return res;
                }
            }
            set { names = value; }
        }

        /// <summary>
        /// Gets or sets the colors.
        /// </summary>
        /// <value>The colors.</value>
        [Browsable(false)]
        public List<string> Colors
        {
            get
            {
                if (this.Values.Count == 0)
                    return colors;
                else
                {
                    List<string> res = new List<string>();
                    foreach (NameColorPair pair in this.Values)
                        res.Add(XmlDataConverter.SerializeColor(pair.Color));
                    return res;
                }
            }
            set { colors = value; }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ChangeVerticesFillColor"/> class.
        /// </summary>
        /// <param name="vertices">The vertices.</param>
        /// <param name="colors">The colors.</param>
        /// <param name="effect">The effect.</param>
        public ChangeVerticesFillColor(IEnumerable<int> vertices, List<string> colors, DrawEffect effect)
        {
            names = new List<string>();            
            foreach (int v in vertices)
                names.Add(((int)(v + 1)).ToString());
            Colors = colors;
            Effect = effect;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ChangeVerticesFillColor"/> class.
        /// </summary>
        public ChangeVerticesFillColor()
        {
        }

        /// <summary>
        /// Runs this instance.
        /// </summary>
        public override void Run()
        {
            old = new List<Color>();
            old.Clear();
            for (int i = 0; i < Names.Count; i++)
            {
                LCVertex v = this.Context.GetVertex(Names[i]);
                old.Add(v.FillColor);
                v.FillColor = XmlDataConverter.DeserializeColor(Colors[i]);
                v.Updated = true;
            }
        }

        /// <summary>
        /// Undoes this instance.
        /// </summary>
        public override void Undo()
        {
            for (int i = 0; i < Names.Count; i++)
            {
                LCVertex v = this.Context.GetVertex(Names[i]);                
                v.FillColor = old[i];
                v.Updated = true;
            }
        }
    }
}
